/*
 * Copyright(c) 2018-? the original author[Wien(luweien@aliyun.com)] or authors.
 *
 */
package org.doll.it.base.enums;

/**
 * 状态码/异常码，默认包含http状态码
 * 1-999: http状态码
 * 1000-1999: 应用公共编码
 * >=2000: 业务编码
 * 
 * @author Wien(luweien@aliyun.com)
 *
 */
public enum CodeEnum {

	/** 空指针异常  */
	NULL_POINT_EXCEPTION(1000, "Null Pointer Exception"),

	/** 参数错误 */
	PARAMETER_ERROR(1001, "Parameter Error"),

	/** ID格式错误 */
	ID_FORMAT_ERROR(1002, "ID Format Error"),

	/** 对象不存在 */
	OBJECT_NOT_EXIST(1003, "Object Not Exist"),

	/** 不能删除正在使用的对象 */
	CANNOT_DELETE_USING_OBJECT(1004, "Cannot Delete Using Object"),

	/** 配置key不存在 */
	CONFIG_KEY_NOT_EXIST(1005, "Config Key Not Exist"),

	// 1xx Informational

	/**
	 * {@code 100 Continue}.
	 */
	CONTINUE(100, "Continue"),
	/**
	 * {@code 101 Switching Protocols}.
	 */
	SWITCHING_PROTOCOLS(101, "Switching Protocols"),
	/**
	 * {@code 102 Processing}.
	 */
	PROCESSING(102, "Processing"),
	/**
	 * {@code 103 Checkpoint}.
	 */
	CHECKPOINT(103, "Checkpoint"),

	// 2xx Success

	/**
	 * {@code 200 OK}.
	 */
	OK(200, "OK"),
	/**
	 * {@code 201 Created}.
	 */
	CREATED(201, "Created"),
	/**
	 * {@code 202 Accepted}.
	 */
	ACCEPTED(202, "Accepted"),
	/**
	 * {@code 203 Non-Authoritative Information}.
	 */
	NON_AUTHORITATIVE_INFORMATION(203, "Non-Authoritative Information"),
	/**
	 * {@code 204 No Content}.
	 */
	NO_CONTENT(204, "No Content"),
	/**
	 * {@code 205 Reset Content}.
	 */
	RESET_CONTENT(205, "Reset Content"),
	/**
	 * {@code 206 Partial Content}.
	 */
	PARTIAL_CONTENT(206, "Partial Content"),
	/**
	 * {@code 207 Multi-Status}.
	 */
	MULTI_STATUS(207, "Multi-Status"),
	/**
	 * {@code 208 Already Reported}.
	 */
	ALREADY_REPORTED(208, "Already Reported"),
	/**
	 * {@code 226 IM Used}.
	 */
	IM_USED(226, "IM Used"),

	// 3xx Redirection

	/**
	 * {@code 300 Multiple Choices}.
	 */
	MULTIPLE_CHOICES(300, "Multiple Choices"),
	/**
	 * {@code 301 Moved Permanently}.
	 */
	MOVED_PERMANENTLY(301, "Moved Permanently"),
	/**
	 * {@code 302 Found}.
	 */
	FOUND(302, "Found"),
	/**
	 * {@code 303 See Other}.
	 */
	SEE_OTHER(303, "See Other"),
	/**
	 * {@code 304 Not Modified}.
	 */
	NOT_MODIFIED(304, "Not Modified"),
	/**
	 * {@code 307 Temporary Redirect}.
	 */
	TEMPORARY_REDIRECT(307, "Temporary Redirect"),
	/**
	 * {@code 308 Permanent Redirect}.
	 */
	PERMANENT_REDIRECT(308, "Permanent Redirect"),

	// --- 4xx Client Error ---

	/**
	 * {@code 400 Bad Request}.
	 */
	BAD_REQUEST(400, "Bad Request"),
	/**
	 * {@code 401 Unauthorized}.
	 */
	UNAUTHORIZED(401, "Unauthorized"),
	/**
	 * {@code 402 Payment Required}.
	 */
	PAYMENT_REQUIRED(402, "Payment Required"),
	/**
	 * {@code 403 Forbidden}.
	 */
	FORBIDDEN(403, "Forbidden"),
	/**
	 * {@code 404 Not Found}.
	 */
	NOT_FOUND(404, "Not Found"),
	/**
	 * {@code 405 Method Not Allowed}.
	 */
	METHOD_NOT_ALLOWED(405, "Method Not Allowed"),
	/**
	 * {@code 406 Not Acceptable}.
	 */
	NOT_ACCEPTABLE(406, "Not Acceptable"),
	/**
	 * {@code 407 Proxy Authentication Required}.
	 */
	PROXY_AUTHENTICATION_REQUIRED(407, "Proxy Authentication Required"),
	/**
	 * {@code 408 Request Timeout}.
	 */
	REQUEST_TIMEOUT(408, "Request Timeout"),
	/**
	 * {@code 409 Conflict}.
	 */
	CONFLICT(409, "Conflict"),
	/**
	 * {@code 410 Gone}.
	 */
	GONE(410, "Gone"),
	/**
	 * {@code 411 Length Required}.
	 */
	LENGTH_REQUIRED(411, "Length Required"),
	/**
	 * {@code 412 Precondition failed}.
	 */
	PRECONDITION_FAILED(412, "Precondition Failed"),
	/**
	 * {@code 413 Payload Too Large}.
	 */
	PAYLOAD_TOO_LARGE(413, "Payload Too Large"),
	/**
	 * {@code 414 URI Too Long}.
	 */
	URI_TOO_LONG(414, "URI Too Long"),
	/**
	 * {@code 415 Unsupported Media Type}.
	 */
	UNSUPPORTED_MEDIA_TYPE(415, "Unsupported Media Type"),
	/**
	 * {@code 416 Requested Range Not Satisfiable}.
	 */
	REQUESTED_RANGE_NOT_SATISFIABLE(416, "Requested range not satisfiable"),
	/**
	 * {@code 417 Expectation Failed}.
	 */
	EXPECTATION_FAILED(417, "Expectation Failed"),
	/**
	 * {@code 418 I'm a teapot}.
	 */
	I_AM_A_TEAPOT(418, "I'm a teapot"),
	/**
	 * {@code 422 Unprocessable Entity}.
	 */
	UNPROCESSABLE_ENTITY(422, "Unprocessable Entity"),
	/**
	 * {@code 423 Locked}.
	 */
	LOCKED(423, "Locked"),
	/**
	 * {@code 424 Failed Dependency}.
	 */
	FAILED_DEPENDENCY(424, "Failed Dependency"),
	/**
	 * {@code 426 Upgrade Required}.
	 */
	UPGRADE_REQUIRED(426, "Upgrade Required"),
	/**
	 * {@code 428 Precondition Required}.
	 */
	PRECONDITION_REQUIRED(428, "Precondition Required"),
	/**
	 * {@code 429 Too Many Requests}.
	 */
	TOO_MANY_REQUESTS(429, "Too Many Requests"),
	/**
	 * {@code 431 Request Header Fields Too Large}.
	 */
	REQUEST_HEADER_FIELDS_TOO_LARGE(431, "Request Header Fields Too Large"),
	/**
	 * {@code 451 Unavailable For Legal Reasons}.
	 */
	UNAVAILABLE_FOR_LEGAL_REASONS(451, "Unavailable For Legal Reasons"),

	// --- 5xx Server Error ---

	/**
	 * {@code 500 Internal Server Error}.
	 */
	INTERNAL_SERVER_ERROR(500, "Internal Server Error"),
	/**
	 * {@code 501 Not Implemented}.
	 */
	NOT_IMPLEMENTED(501, "Not Implemented"),
	/**
	 * {@code 502 Bad Gateway}.
	 */
	BAD_GATEWAY(502, "Bad Gateway"),
	/**
	 * {@code 503 Service Unavailable}.
	 */
	SERVICE_UNAVAILABLE(503, "Service Unavailable"),
	/**
	 * {@code 504 Gateway Timeout}.
	 */
	GATEWAY_TIMEOUT(504, "Gateway Timeout"),
	/**
	 * {@code 505 HTTP Version Not Supported}.
	 */
	HTTP_VERSION_NOT_SUPPORTED(505, "HTTP Version not supported"),
	/**
	 * {@code 506 Variant Also Negotiates}
	 */
	VARIANT_ALSO_NEGOTIATES(506, "Variant Also Negotiates"),
	/**
	 * {@code 507 Insufficient Storage}
	 */
	INSUFFICIENT_STORAGE(507, "Insufficient Storage"),
	/**
	 * {@code 508 Loop Detected}
	 */
	LOOP_DETECTED(508, "Loop Detected"),
	/**
	 * {@code 509 Bandwidth Limit Exceeded}
	 */
	BANDWIDTH_LIMIT_EXCEEDED(509, "Bandwidth Limit Exceeded"),
	/**
	 * {@code 510 Not Extended}
	 */
	NOT_EXTENDED(510, "Not Extended"),
	/**
	 * {@code 511 Network Authentication Required}.
	 */
	NETWORK_AUTHENTICATION_REQUIRED(511, "Network Authentication Required");

	private final int code;

	private final String message;

	private CodeEnum(int code, String message) {
		this.code = code;
		this.message = message;
	}

	/**
	 * @return Return the integer value of this status code.
	 */
	public int getCode() {
		return code;
	}

	/**
	 * @return Return the reason phrase of this code.
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * Return the enum constant of this type with the specified numeric value.
	 * @param code the numeric value of the enum to be returned
	 * @return the enum constant with the specified numeric value
	 * @throws IllegalArgumentException if this enum has no constant for the specified numeric value
	 */
	public static CodeEnum valueOf(int code) {
		for (CodeEnum codeEnum : values()) {
			if (codeEnum.getCode() == code) {
				return codeEnum;
			}
		}
		throw new IllegalArgumentException("No matching constant for [" + code + "]");
	}

	/**
	 * 获取BaseCode中的消息
	 * @return
	 */
	public static String ofMessage(CodeEnum codeEnum) {
		return ofMessage(codeEnum, null);
	}

	/**
	 * 获取BaseCode中的消息，如果有额外的信息，并追加上
	 * @return
	 */
	public static String ofMessage(CodeEnum codeEnum, String extraMsg) {
		String meesage = null;
		if (codeEnum != null && codeEnum.getMessage() != null) {
			meesage = codeEnum.getMessage();
		}
		if (extraMsg == null) {
			return meesage;
		}
		if (meesage == null) {
			return extraMsg;
		}
		return meesage += ": " + extraMsg;
	}

	/**
	 * 获取BaseCode中的Code
	 * @return
	 */
	public static Integer ofCode(CodeEnum codeEnum) {
		return (codeEnum == null) ? null : codeEnum.getCode();
	}
}
